package com.mmm.user.controller;

import com.mmm.common.core.interceptor.ScopeLevel;
import com.mmm.user.dto.TokenDto;
import com.mmm.user.entity.User;
import com.mmm.user.service.JwtService;
import com.mmm.user.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @email 1067559930@qq.com
 * @author: One
 * @date: 2022/02/28
 * @time: 16:07
 */
@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private JwtService jwtService;

    @PostMapping
    public Map<String, String> login(@RequestBody User user) {
        String jwt = tokenService.code2Session(user);
        HashMap<String, String> token = new HashMap<>();
        token.put("token", jwt);
        return token;
    }

    @PostMapping("/verify")
    @ScopeLevel
    public Boolean verifyJwt(@RequestBody TokenDto token) {
        return jwtService.verify(token.getToken());
    }

    @PostMapping("/getClaim")
    public Map<String,String> getClaim(@RequestHeader("Authorization") String authorization){
       return jwtService.getClaimMap(authorization);
    }

}
